import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;


public class DFT
{
	/*
	private native void print();

	static
	{
		System.loadLibrary("compute_dft");
	}*/
	
	public static void main(String[] args)
	{
				
		String inFileName = null;
		int frequency = 0;//variable to hold 
		int [] arr = new int[4096];//array to hold dft samples
		Scanner scan = null;


		
		if(args.length == 2)
		{
			inFileName = args[0];
			
			try {
		        frequency = Integer.parseInt(args[1]);
		    } catch (NumberFormatException e) {
		        System.err.println("Second argument must be an integer!");
		        System.exit(1);
		    }
			
		}
		
		else
		{
			System.err.println("Need two command line parameters to \"dft\"");	
			System.exit(1);
		}
		
		System.out.println("Got command line parameters!");
		System.out.println("File name is: " + inFileName);
		System.out.println("Frequency is: "+ frequency);
		
		//////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////
		//////Create test file of 4096 numbers and write to file
		System.out.println("Making Random Numbers!!");
		int minRangeInt = 0;    // This creates the base of the range 
		int maxRangeInt = 8000;  // This creates the top of the range 
		int randomNumberInt = 0;
		int counter = 0;
		// Create infinite loop
		File file = new File(inFileName);
		Writer w = null;
		try {
			w = new FileWriter(file);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			System.err.println("Cannot open file to write to!");
		}
		while(counter<4096) { 
			// Update the value of randomNumberInt 
			randomNumberInt = (minRangeInt + (int)(Math.random() * (maxRangeInt - minRangeInt)));
			// Print the updated value of randomNumberInt 
			try {
				w.write(new Integer(randomNumberInt).toString()+ " ");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.err.println("Error Generating Test File!!");
			}
			//might want to write String.valueOf(randomNumberInt)??
			counter++;
		}//while
			try {
				w.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				System.err.println("Cannot Close File correctly!");
			}
		//////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////	
		
		
		try {
			scan = new Scanner(new BufferedReader(new FileReader(inFileName)));
		} catch (FileNotFoundException e) {
			System.err.println("Cannot read input file!");
		}
		int i = 0;
		
		//while the file has int's to read and is less than 4096
		//fill the array with the samples.
		while(scan.hasNextInt() && i<4096)
		{
			arr[i] = scan.nextInt();
			i++;
		}//while
		
		//for(int j = 0; j<i; j++)
			//System.out.println("Arr @ "+ j + ": "+arr[j]);
		
		
	scan.close();
	
	double dft_result = dft(arr, 440);
	
	System.out.println("DFT Result is: "+dft_result);
	
	System.exit(1);
		
	
	}//main
	
	public static double dft(int [] arr, int frequency)
	{
		double a_sum = 0;
		double b_sum = 0;
		for(int i = 0; i<arr.length; i++)
		{
			a_sum = arr[i] * Math.cos((2 * Math.PI * i * frequency)/arr.length);
			
			b_sum = arr[i] * Math.sin((2 * Math.PI * i * frequency/arr.length));
			
		}//for
		double result = Math.sqrt(Math.pow(a_sum, 2)+Math.pow(b_sum, 2));
		
		return result;
		
	}//dft
	
}//class
